package com.fish.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fish.common.annotation.RequiresPermission;
import com.fish.common.result.Result;
import com.fish.dao.mapper.OperationLogMapper;
import com.fish.model.entity.OperationLog;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 操作日志Controller
 */
@Tag(name = "操作日志管理", description = "系统操作日志查询相关接口")
@Slf4j
@RestController
@RequestMapping("/api/operation-log")
public class OperationLogController {
    
    @Autowired
    private OperationLogMapper operationLogMapper;
    
    /**
     * 分页查询操作日志
     */
    @GetMapping("/list")
    @RequiresPermission("system:log:list")
    public Result<Page<OperationLog>> getLogList(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String result
    ) {
        Page<OperationLog> page = new Page<>(current, size);
        LambdaQueryWrapper<OperationLog> wrapper = new LambdaQueryWrapper<>();
        
        // 条件查询
        if (StringUtils.hasText(username)) {
            wrapper.like(OperationLog::getUsername, username);
        }
        if (StringUtils.hasText(result)) {
            wrapper.eq(OperationLog::getResult, result);
        }
        
        // 按时间倒序
        wrapper.orderByDesc(OperationLog::getCreateTime);
        
        Page<OperationLog> logPage = operationLogMapper.selectPage(page, wrapper);
        return Result.success(logPage);
    }
    
    /**
     * 根据ID查询操作日志详情
     */
    @GetMapping("/{id}")
    @RequiresPermission("system:log:detail")
    public Result<OperationLog> getLogDetail(@PathVariable Long id) {
        OperationLog log = operationLogMapper.selectById(id);
        return Result.success(log);
    }
}